package com.hnqcgc.db;

import java.sql.Connection;
import java.sql.SQLException;

public class MyThansaction {
    private Connection connection;
// 为 true 表示启用自动提交模式；为 false 表示禁用自动提交模式
    public MyThansaction(boolean isAuto) {
        try {
            this.connection =new MyConn().getConn() ;
            this.connection.setAutoCommit(isAuto);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //手动提交方法
    public void commit(){
        if(connection!=null){
            try {
                connection.commit();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //手动回滚方法
    public void rollback(){
        if(connection!=null){
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    //获得数据库连接的方法
    public Connection getConnection(){
        return this.connection;
    }
    //关闭数据库连接方法
    public void closeConn(){
        try {
            if(connection!=null&&!connection.isClosed()){
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
